#pragma once

#include "timercpp.h"
#include <atomic>
#include <functional>

typedef std::function<void()> timeout_callback;

class TimeOutManager
{
public:
    TimeOutManager()
        :m_timeout_flag(true), m_cb(nullptr)
    {}

    ~TimeOutManager()
    {}

    void StartOnce(timeout_callback cb,  int millseconds)
    {
        m_timeout_flag = true;
        m_cb = cb;
        m_timeout_timer.setTimeout(std::bind(&TimeOutManager::timeout, this), millseconds);
    }

    void SetNoTimeOut()
    {
        m_timeout_flag = false;
    }
private:
    void timeout()
    {
        if (m_timeout_flag)
        {
            if (m_cb)
            {
                m_cb();
            }
        }
    }

private:
    Timer m_timeout_timer;

    std::atomic<bool> m_timeout_flag;

    timeout_callback m_cb;
};